package com.bwie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bwie.config.OSSFileUtil;
import com.bwie.op.Type;
import com.bwie.pojo.EsLoan;
import com.bwie.pojo.Loan;
import com.bwie.service.LoanService;
import com.bwie.util.R;
import com.bwie.vo.LoanVo;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author 国兴越
 * @version 1.0
 * Create by
 */
@RestController
@RequestMapping("loan")
public class LoanController {


    @Autowired
    LoanService loanService;

    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Autowired
    FastFileStorageClient fastFileStorageClient;

    @Autowired
    Type type;

    @Autowired
    OSSFileUtil ossFileUtil;

    @Autowired
    private static final String URL="http://192.168.80.128:8888/";


    @PostMapping("/uploadFile")
    public R uploadFile(MultipartFile file) throws IOException {

        String s = ossFileUtil.uploadFile(file);
        return R.ok().message("图片上传成功").data("s",s);
    }




    @PostMapping("create")
    public R create(){

        IndexOperations indexOps = elasticsearchRestTemplate.indexOps(EsLoan.class);
        indexOps.create();
        Document mapping = indexOps.createMapping();
        indexOps.putMapping(mapping);
        return R.ok().message("索引创建成功");

    }
    @PostMapping("/list")
    public R list(@RequestBody LoanVo loanVo){
        return loanService.lists(loanVo);
    }


    @PostMapping("/upload")
    public R upload(MultipartFile file) throws IOException {
        String s = StringUtils.substringAfterLast(file.getOriginalFilename(), ".");
        String fullPath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), s, null).getFullPath();
        fullPath=URL+fullPath;
        return R.ok().message("图片上传成功").data("fullPath",fullPath);
    }

    @PostMapping("/save")
    public R  save(@RequestBody LoanVo loanVo){

        return loanService.add(loanVo);

    }

    @PostMapping("/periods")
    public R periodsList(){
       return type.periodsList();
    }

    @PostMapping("/repay")
    public R repayList(){
        return type.repayList();
    }

    @PostMapping("/time")
    public R timeList(){
        return type.timeList();
    }

    @PostMapping("/type")
    public R TypeList(){
        return type.TypeList();
    }


    @PostMapping("/approve/{id}")
    public R approve(@PathVariable("id") Integer id){




        return loanService.selectById(id);




    }








}
